package com.gopay.common.user.dao.impl;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.domain.aggregatepayment.ApContractInfo;
import com.gopay.common.domain.cust.CustSettledConfigInfo;
import com.gopay.common.user.dao.CustSettledConfigInfoDao;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lij-jiang
 * @ClassName: CustSettledConfigInfoDaoImpl.java
 * @Description: 民生公众号支付商户配置Dao
 * @date 2017年07月21日 15:43
 */
@Repository(value = "custSettledConfigInfoDao")
public class CustSettledConfigInfoDaoImpl extends BaseDAOImpl<CustSettledConfigInfo, Serializable> implements CustSettledConfigInfoDao {

    @Override
    public List<CustSettledConfigInfo> findSettledConfigInfoByCustId(String custId) {
        StringBuffer hql = new StringBuffer();
        Map<String, Object> map = new HashMap<>();
        hql.append("from CustSettledConfigInfo where  1= 1 and custId =:custId order by createdTs ");
        map.put("custId", custId);
        return findByHql(hql.toString(), map);
    }
	@Override
	public List<CustSettledConfigInfo> findSettledConfigInfo(String custId, String busType,String channType) {
		  StringBuffer hql = new StringBuffer();
	        Map<String, Object> map = new HashMap<>();
	        hql.append("from CustSettledConfigInfo where  1= 1 and custId =:custId and status ='01' and businessType=:businessType and channelType =:channelType");
	        map.put("custId", custId);
	        map.put("businessType", busType);
	        map.put("channelType", channType);
	        return findByHql(hql.toString(),map);
	}
    @Override
    public List<CustSettledConfigInfo> findConfigInfoByCustIdAndChannelType(String custId, String channelType) {
        StringBuffer hql = new StringBuffer();
        Map<String, Object> map = new HashMap<>();
        hql.append("from CustSettledConfigInfo where  1= 1 and custId =:custId and channelType =:channelType " +
                "order by createdTs ");
        map.put("custId", custId);
        map.put("channelType", channelType);
        return findByHql(hql.toString(), map);
    }

    @Override
    public List<CustSettledConfigInfo> findSettledConfigInfo(String custId, String busType) {
        StringBuffer hql = new StringBuffer();
        Map<String, Object> map = new HashMap<>();
        hql.append("from CustSettledConfigInfo where  1= 1 and custId =:custId and businessType=:businessType ");
        map.put("custId", custId);
        map.put("businessType", busType);
        return findByHql(hql.toString(), map);
    }

    @Override
    public CustSettledConfigInfo findSettledKey(String custId) {
        StringBuilder hql = new StringBuilder();
        Map<String, Object> map = new HashMap<>();
        hql.append("from CustSettledConfigInfo where  1= 1 and custId =:custId and businessType = '4'");
        map.put("custId", custId);
        List<CustSettledConfigInfo> list = findByHql(hql.toString(), map);
        return list.size() == 0 ? null : list.get(0);
    }

    @Override
    public void updateCustSettledKey(Map<String, Object> param) {
        StringBuilder sql = new StringBuilder();
        sql.append("update cust_settled_config_info set business_value =:newKey ," +
                "last_upd_oper_id =:lastUpdOperId,last_upd_ts=:lastUpdTs where id=:id");
        updateBySql(sql.toString(), param);
    }

    @Override
    public List<CustSettledConfigInfo> findConfigInfoByCustIdAndStatus(String custId, String status) {
        StringBuffer hql = new StringBuffer();
        Map<String, Object> map = new HashMap<>();
        hql.append("from CustSettledConfigInfo where  1= 1 and custId =:custId and status=:status order by createdTs ");
        map.put("custId", custId);
        map.put("status", status);
        return findByHql(hql.toString(), map);
    }

    @Override
    public List<CustSettledConfigInfo> findSettledConfigInfos(CustSettledConfigInfo configInfo) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(CustSettledConfigInfo.class);
        if(StringUtils.isNotBlank(configInfo.getCustId())){
            detachedCriteria.add(Restrictions.eq("custId", configInfo.getCustId()));
        }

        if(StringUtils.isNotBlank(configInfo.getBusinessType())) {
            detachedCriteria.add(Restrictions.eq("businessType", configInfo.getBusinessType()));
        }

        if(StringUtils.isNotBlank(configInfo.getChannelType())) {
            detachedCriteria.add(Restrictions.eq("channelType", configInfo.getChannelType()));
        }

        return findByCriteria(detachedCriteria);
    }
}
